// lientSocket1.cpp : implementation file
//

#include "stdafx.h"

#include "MFCSocket_Server.h"
#include "ClientSocket1.h"
#include "../MyInclude/ClientInfo.h"

// ClientSocket1
static CString Int2String(int n);

ClientSocket1::ClientSocket1()
{
	//CString s=Int2String(-123456);
}

ClientSocket1::~ClientSocket1()
{
}

void ClientSocket1::OnAccept(int nErrorCode)
{
	OutputDebugString(_T("[ClientSocket1]: OnAccept!"));
	CSocket::OnAccept(nErrorCode);
}
static int k = 0;
static CString Int2String(int n)
{
	char NUM[] = {'0','1','2','3','4','5','6','7','8','9'};
	//bool isNegative = false;
	int index = 0;
	char res[256];
	if(n < 0)
	{
		index = 1;
		//isNegative = true;
		n = -n;
		res[0] = '-';
	}
	
	while(n>0)
	{
		res[index] = NUM[n%10];
		n /= 10;
		index++;
	}
	int k = 0;
	if(res[0] == '-')
	{
		k=1;
	}
	for(int i=k; i<(index-k)/2+k; i++)
	{
		char tmp = res[i];
		res[i] = res[index-i-1+k];
		res[index-i-1+k] = tmp;
	}
	res[index] = '\0';
	CString s(res);
	return s;
}
void ClientSocket1::OnReceive(int nErrorCode)
{
	OutputDebugString(_T("[ClientSocket1]: OnReceive!"));
	char buff[1000];
	int br = Receive(buff, 1000);
	/*
	buff[br] = (char)k;
	k++;
	buff[br+1] = '\0';
	*/
	k++;
	/*
	CString s(_T(""));
	s.Format(_T("%s %d"),buff,k);
	OutputDebugString(s);
	*/
	buff[br] = '\0';
	OutputDebugStringA(buff);
	CSocket::OnReceive(nErrorCode);

	ClientInfo info;
	info.GetInfo(buff);

	char sSend[256];
	if(strcmp(info.sMsg,"abc")==0)
	{
		strcpy(sSend,"Da nhan dc");
		Send(sSend, sizeof(sSend));
	}
}
